/*! ******************************************************************************
 *
 * Created on 2022年3月25日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.yzbdl.lanius.ui.exception;

import org.eclipse.swt.SWT;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleExceptionHandler;
import org.yzbdl.lanius.core.exception.LaniusServerErrorException;
import org.pentaho.di.ui.core.dialog.ShowMessageDialog;
import org.pentaho.di.ui.spoon.Spoon;
import org.yzbdl.lanius.core.net.HttpStatus;

/**
 * @author hs@yzbdl.ac.cn
 *
 */
public class ServerErrorExceptionHandler extends KettleExceptionHandler {

    @Override
    public boolean isAccept(KettleException e) {
        if (e instanceof LaniusServerErrorException) {
            return ((LaniusServerErrorException)e).getResponseCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR;
        }
        return false;
    }

    @Override
    public boolean isThrowOut() {
        return true;
    }

    @Override
    public void process(KettleException e) {
        String message = ((LaniusServerErrorException)e).getResponseMessage();
        ShowMessageDialog dialog =
            new ShowMessageDialog(Spoon.getInstance().getShell(), SWT.OK | SWT.ICON_ERROR, "提示", message, 300, 150);
        dialog.open();
    }

}
